using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using MvcApplication2.Models;

namespace MvcApplication2.Controllers
{
    public sealed class UserSettingViewModel : UserProfile
    {
        public UserSettingViewModel()
        {

        }
        public void CreateCheckBoxList(MvcApplication2Context db)
        {
            //User = user;
            AvailableLanguage = new List<Language>(db.Languages.ToList());
            SelectedLanguages = new List<Language>();
            foreach (var language in Languages)
            {
                SelectedLanguages.Add(language);
            }

            AvailableDevices = new List<Device>(db.Devices.ToList());
            SelectedDevices = new List<Device>();
            foreach (var device in Devices)
            {
                SelectedDevices.Add(device);
            }
        }

        //public int UserId { get; set; }
        //public User User { get; set; }


        public IList<Language> AvailableLanguage { get; set; }
        public IList<Language> SelectedLanguages { get; set; }
        public PostedLanguage PostedLanguages { get; set; }

        public IList<Device> AvailableDevices { get; set; }
        public IList<Device> SelectedDevices { get; set; }
        public PostedDevice PostedDevices { get; set; }

        public class PostedLanguage
        {
            [Display(Name = "User Languages")]
            public string[] Languages { get; set; }
        }
        public class PostedDevice
        {
            [Display(Name = "Devices")]
            public string[] Devices { get; set; }
        }

        internal void Update(UserProfile userm, MvcApplication2Context db)
        {
            /*
             * Non cambio username nel membership provider.
             */
            userm.PayPalMail = PayPalMail;
            userm.CanPcScreencast = CanPcScreencast;
            userm.CanRegisterMobileDevice = CanRegisterMobileDevice;
            userm.AcceptTestingRequest = AcceptTestingRequest;

            while (userm.Languages.Count > 0)
            {
                userm.Languages.Remove(userm.Languages.First());
            }

            if (PostedLanguages != null)
                foreach (var lang in PostedLanguages.Languages)
                {
                    var languageid = int.Parse(lang);

                    userm.Languages.Add(db.Languages.Find(languageid));
                }

            while (userm.Devices.Count > 0)
            {
                userm.Devices.Remove(userm.Devices.First());
            }

            if (PostedDevices != null)
                foreach (var dev in PostedDevices.Devices)
                {
                    var deviceId = int.Parse(dev);

                    userm.Devices.Add(db.Devices.Find(deviceId));
                }
        }

    }
}